package ch.bfh.swos.learnaid.model;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Persistence;
import javax.persistence.Query;

import org.junit.Assert;
import org.junit.Test;

public class DeleteIT {

	@Test
	public void test() {
		EntityManager em = Persistence.createEntityManagerFactory(
				"ch.bfh.swos.learnaid.domain").createEntityManager();

		Query q = em.createQuery("select a from Author a");
		@SuppressWarnings("unchecked")
		List<Author> foundAuthors = q.getResultList();
		Author firstAuthor = foundAuthors.get(0);
		Assert.assertTrue(firstAuthor.getLastname().equals("OtherName"));

		// Write access needs a transaction
		em.getTransaction().begin();
		em.remove(firstAuthor);
		em.getTransaction().commit();
		
		Query q2 = em.createQuery("select a from Author a");
		@SuppressWarnings("unchecked")
		List<Author> emptyAuthors = q2.getResultList();
		Assert.assertTrue(emptyAuthors.size()==0);
		
	}

}
